package com.jlastu.dlxy_androidapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Product1;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
    private List<Product1> products;
    private boolean inEditMode = false;


    public void setInEditMode(boolean inEditMode) {
        this.inEditMode = inEditMode;
    }
    //管理按钮
    public boolean isInEditMode() {
        return inEditMode;
    }
    public static class ProductViewHolder extends RecyclerView.ViewHolder {
        public CheckBox productCheckBox;
        public ImageView productImage;
        public TextView productName;
        public TextView productPrice;
        public Button decreaseButton;
        public TextView quantityTextView;
        public Button increaseButton;
        public View deleteButton;

        public ProductViewHolder(View itemView) {
            super(itemView);
            productCheckBox = itemView.findViewById(R.id.productCheckBox);
            productImage = itemView.findViewById(R.id.productImage);
            productName = itemView.findViewById(R.id.productName);
            productPrice = itemView.findViewById(R.id.productPrice);
            decreaseButton = itemView.findViewById(R.id.decreaseButton);
            quantityTextView = itemView.findViewById(R.id.quantityTextView);
            increaseButton = itemView.findViewById(R.id.increaseButton);
        }
    }

    public ProductAdapter(List<Product1> products) {
        this.products = products;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        ProductViewHolder viewHolder = new ProductViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        Product1 product = products.get(position);

        if (isInEditMode()) {
            holder.deleteButton.setVisibility(View.VISIBLE);
            holder.deleteButton.setOnClickListener(view -> {
                // 删除商品的逻辑
                products.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, products.size());
                // 其他操作，更新合计金额等
            });
        } else {
            holder.deleteButton.setVisibility(View.GONE);
        }

        holder.productCheckBox.setChecked(product.isSelected());
        // 设置商品图片、名称、价格等信息
        holder.decreaseButton.setOnClickListener(view -> {
            // 减少商品数量的逻辑

            int quantity = product.getQuantity();
            if (quantity > 1) {
                product.setQuantity(quantity - 1);
                notifyDataSetChanged();
            }

        });


        holder.increaseButton.setOnClickListener(view -> {
            // 增加商品数量的逻辑
            int quantity = product.getQuantity();
            product.setQuantity(quantity + 1);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return products.size();
    }
}
